package com.jiepos.mpos.common.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.AppCodeListDao;

/**
 * 
 * <p>标题: —— 系统服务：用于系统缓存数据的查询</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @author 娄伟峰
 * @version 1.0
 * @since 2017年4月18日 下午2:05:04
 * @history（历次修订内容、修订人、修订时间等）
 */
@Repository
public class SystemService {
	private final Logger log = LoggerFactory.getLogger(SystemService.class);

 
	@Autowired
	private AppCodeListDao appCodeListDao;
	@Autowired
	private RedisTemplate redisTemplate;
  
	/**
	 * 启动时初始化生成机构类别集合
	 * @return
	 */
	public  Map getAllCodeList(){
		String sql = "select distinct(code_tp) as code_tp from "+Table.T_APP_CODE_LIST+" where rec_st!='0'";
		List<Map> codeTpList = appCodeListDao.queryForList(sql,null);
		
		String allSql = "select * from "+Table.T_APP_CODE_LIST+" where rec_st!='0'";
		List<Map> allCodeList = appCodeListDao.queryForList(allSql,null);
		Map codeTpMap = null;
		String codeTp = null;
		String codeTp2 = null;
		Map map = null;
		Map dataMap = null;
		List dataList = null;
		Map returnMap = new HashMap();
		for(int i = 0 ; i < codeTpList.size() ; i++){
			//循环所有类型
			dataList = new ArrayList();
			codeTpMap = codeTpList.get(i);
			codeTp = String.valueOf(codeTpMap.get("code_tp"));
			for(int j = 0 ; j < allCodeList.size() ; j++){
				//取出某个类型所有的数据
				dataMap = new HashMap();
				map = allCodeList.get(j);
				codeTp2 = String.valueOf(map.get("code_tp"));
				if(codeTp.equals(codeTp2)){
					dataMap.put("code_val",String.valueOf(map.get("code_val")));
					dataMap.put("code_desc",String.valueOf(map.get("code_desc")));
					dataList.add(dataMap);
				}
			}
			//类型对应集合
			returnMap.put(codeTp, dataList);
		}
		return returnMap; 
	}
	/**
	 * 启动时初始化生成菜单URL集合
	 */
	public List<Map> getAllAuthMenuList(){
		List list = new ArrayList();
/*		String menuSql = "select menu_uuid as id,menu_url from "+SystemConstant.T_BILL_MENU_CD;
		String buttonSql = "select button_uuid as id,button_url from "+SystemConstant.T_BILL_MENUBUTTON_INF;
		List<Map> allButtonMenuList = billCodeListDao.queryForList(buttonSql,null);
		List<Map> allAuthMenuList = billCodeListDao.queryForList(menuSql,null);
	
		Map map = null;
		for(int i=0;i<allButtonMenuList.size();i++){
			map = allButtonMenuList.get(i);
			list.add(map.get("button_url"));
		}
		for(int i=0;i<allAuthMenuList.size();i++){
			map = allAuthMenuList.get(i);
			list.add(map.get("menu_url"));
		}
		//SystemConstant.All_URLLIST = list;
*/		return list;
	}
	  
	/**
	 * 获取所有的uuid以及对应的URL
	 * @param menuUUID
	 * @return
	 */
	public Map getAllUrl(){
		/*String sql = "select menu_uuid,menu_url from "+SystemConstant.T_BILL_MENU_CD+" where rec_st!='0'";
		List<Map> urlList = billCodeListDao.queryForList(sql,null);
		Map map = null;
		Map newMap = new HashMap();
		for(int i = 0;i < urlList.size();i++){
			map = urlList.get(i);
			newMap.put(map.get("menu_uuid"), map.get("menu_url"));
		}
		return newMap;*/
		return null;
	}
	
	/**
	 * 更新缓存
	 * @param key
	 * @param values
	 */
	public void updateRedis(String key,Map values){
		redisTemplate.opsForHash().putAll(key, values);
	}
	
	/**
	 * 删除缓存
	 * @param key
	 */
	public void deleteRedis(String key){
		redisTemplate.delete(key);
	}
	
	/**
	 * 存放hash的值
	 * @param hKey
	 * @param mKey
	 * @param value
	 */
	public void putHash(String hKey, String mKey, Object value){
		redisTemplate.opsForHash().put(hKey, mKey, value);
	}
	
	/**
	 * 获取hash的值
	 * @param hKey
	 * @param mKey
	 * @return
	 */
	public <T> T getHash(String hKey, String mKey){
		return (T)redisTemplate.opsForHash().get(hKey, mKey);
	}
	
	/**
	 * 删除hash的key
	 * @param hkey
	 * @param kyes
	 */
	public void remove(String hkey, String... kyes){
		redisTemplate.opsForHash().delete(hkey, kyes);
	}
	
	
	/**
	 * 获取codelist参数
	 * @param codeTp
	 * @return
	 */
	public List getCodeListByTp(String codeTp){
		HashOperations<String, Object, Object>  hash = redisTemplate.opsForHash();
		Map codeMap = (Map)hash.entries(SystemConstant.CACHE_CODELIST);
		List<Map<String, String>> list = (ArrayList)codeMap.get(codeTp.toUpperCase());
		return list;

	}
	
	
}
